package net.xdclass.jvm;

import java.io.*;

/**
 * @author zhaowei
 * @Description TODO
 * @Remark 有问题直接联系我
 * @Version 1.0
 **/
public class MyClassLoader extends ClassLoader{


    private  String path;

    public MyClassLoader(String path){

    }



    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {

        String fileName = path + name + ".class";
        System.out.println(fileName);

        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
             ByteArrayOutputStream bos = new ByteArrayOutputStream();){

            int len=0;
            byte[] data = new byte[1024];

            while ((len= bis.read(data))!=-1){
                bos.write(data,0,len);
            }

            byte[] bytes = bos.toByteArray();

            Class<?> defineClass = defineClass(null, bytes, 0, bytes.length);
            return defineClass;

        }catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }
}
